/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.classes;

import classes.Breadcrumbs;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Category;
import dao.CategoryDao;
import classes.HibernateUtil;
import classes.Url;
import dao.CourseDao;
import javax.servlet.http.HttpSession;
import models.Course;
import models.User;

/**
 * This servlet handles the manage category page. Also handles the remove and
 * edit method.
 *
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class CategoryController extends HttpServlet {

    /**
     * This do get method handles edit or remove category. Also handles the
     * Category list.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Url.setHistory(request);
        if (request.getParameter("id") != null) { //Edit Category
            editCategory(Long.parseLong(request.getParameter("id")), request, response);
        } else if (request.getParameter("idremove") != null) { //remove category
            removeCategory(Long.parseLong(request.getParameter("idremove")), request, response);
        } else { //Get the category list and redirect to the category page

            HibernateUtil hu = new HibernateUtil();
            CategoryDao ed = new CategoryDao(hu.openSession());
            CourseDao cd = new CourseDao(hu.openSession());
            List<Category> categoryList = ed.findAll();
            List<Course> courseList = cd.findAll();

            HttpSession ses = request.getSession();

            if (ses.getAttribute("user") != null) {
                User user = (User) ses.getAttribute("user");
                if (ses.getAttribute("userrole").equals("Student")) {
                    List<Course> ownedCourses = cd.findAllByStudent(user.getMember_id());
                    request.setAttribute("ownedCourses", ownedCourses);
                }
            }

            RequestDispatcher dispatcher = request.getRequestDispatcher("");

            if (request.getParameter("edit") != null) {
                // Breadcrumbs
                Breadcrumbs[] breadcrumbs = {
                    new Breadcrumbs("Index", "Home"),
                    new Breadcrumbs("Category?edit", "Manage categories and courses")
                };
                request.setAttribute("breadcrumbs", breadcrumbs);

                request.setAttribute("categories", categoryList);
                request.setAttribute("courses", courseList);
                dispatcher = request.getRequestDispatcher("jsp/manage_categories.jsp");
            } else {
                // Breadcrumbs
                Breadcrumbs[] breadcrumbs = {
                    new Breadcrumbs("Index", "Home"),
                    new Breadcrumbs("Category", "Course catalog")
                };
                request.setAttribute("breadcrumbs", breadcrumbs);

                request.setAttribute("categories", categoryList);
                dispatcher = request.getRequestDispatcher("jsp/categories.jsp");
            }
            dispatcher.forward(request, response);
        }
    }
    /*
     * method to edit a category redirects to the add/edit page
     */

    private void editCategory(long id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        CategoryDao ed = new CategoryDao(hu.openSession());

        List<Category> categoryList = ed.findAll();
        Category temp = null;
        for (int i = 0; i < categoryList.size(); i++) {
            if (categoryList.get(i).getId() == id) {
                temp = categoryList.get(i); //Get the category
            }
        }

        if (temp != null) {
            request.setAttribute("categoryedit", temp);
        }

        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Category?edit", "Manage categories and courses"),
            new Breadcrumbs("Category?id", "Edit category")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);

        RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/add_edit_categories.jsp");
        dispatcher.forward(request, response);
    }

    /*
     * Method to delete a category
     */
    private void removeCategory(long id, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HibernateUtil hu = new HibernateUtil();
        CategoryDao cd = new CategoryDao(hu.openSession());
        Category catalogItem = cd.get(id);
        cd.delete(catalogItem);
        hu.close();

        response.sendRedirect("Category?edit");
    }
}
